package com.dd.logistics.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.logistics.common.Result;
import com.dd.logistics.entity.Supplier;
import com.dd.logistics.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/supplier")
public class SupplierController {
    
    @Autowired
    private SupplierService supplierService;
    
    @GetMapping("/list")
    public Result<List<Supplier>> list() {
        try {
            List<Supplier> list = supplierService.list();
            return Result.success(list);
        } catch (Exception e) {
            return Result.error("获取供应商列表失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/page")
    public Result<Page<Supplier>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name) {
        try {
            Page<Supplier> page = new Page<>(current, size);
            QueryWrapper<Supplier> wrapper = new QueryWrapper<>();
            if (name != null && !name.isEmpty()) {
                wrapper.like("name", name);
            }
            supplierService.page(page, wrapper);
            return Result.success(page);
        } catch (Exception e) {
            return Result.error("分页查询供应商失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/get/{name}")
    public Result<Supplier> getSupplierByName(@PathVariable String name) {
        try {
            Supplier supplier = supplierService.getByName(name);
            return supplier != null ? Result.success(supplier) : Result.error("供应商不存在");
        } catch (Exception e) {
            return Result.error("获取供应商信息失败：" + e.getMessage());
        }
    }
    
    @PostMapping("/add")
    public Result<Boolean> addSupplier(@RequestBody Supplier supplier) {
        try {
            if (supplier.getName() == null || supplier.getName().isEmpty()) {
                return Result.error("供应商名称不能为空");
            }
            boolean success = supplierService.save(supplier);
            return success ? Result.success("添加供应商成功", true) : Result.error("添加供应商失败");
        } catch (Exception e) {
            return Result.error("添加供应商失败：" + e.getMessage());
        }
    }
    
    @PutMapping("/update")
    public Result<Boolean> updateSupplier(@RequestBody Supplier supplier) {
        try {
            if (supplier.getId() == null) {
                return Result.error("供应商ID不能为空");
            }
            boolean success = supplierService.updateSupplierInfo(supplier);
            return success ? Result.success("更新供应商信息成功", true) : Result.error("更新供应商信息失败");
        } catch (Exception e) {
            return Result.error("更新供应商信息失败：" + e.getMessage());
        }
    }
    
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteSupplier(@PathVariable Long id) {
        try {
            boolean success = supplierService.removeById(id);
            return success ? Result.success("删除供应商成功", true) : Result.error("删除供应商失败");
        } catch (Exception e) {
            return Result.error("删除供应商失败：" + e.getMessage());
        }
    }
} 